/**
 * 
 */
package com.nari.rescontroller.dao;

import com.nari.basic.dao.IRoleDao;
import com.nari.basic.model.IUser;
import com.nari.basic.utils.sysmgr.UserRoleUtil;
import com.nari.platform.utils.StringUtils;
import com.nari.resconfig.dao.IProjectResourceConfigDao;

/**
 * 
 * @author Xiaopeng
 * @date 2014-5-14
 */
public class ResourceAccessControlDaoImpl implements IResourceAccessControlDao {
	private IProjectResourceConfigDao iProjectResourceConfigDao;
	private IRoleDao iRoleDao;
	private String superManager;
	private String sysadmin;
	private String superSale_siteOperation;
	
	/**
	 * 查询系统登录用户的权限匹配项目资源
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-14
	 */
	public String queryAuthorityProjResHql() throws Exception {
		//查询系统用户的登录权限
		IUser loginUser = UserRoleUtil.getSysLoginUser();
		String loginUserRoleIDList = iRoleDao.queryRoleIDsByUserID(loginUser.getUserID());
		//构造权限匹配的项目资源
		StringBuffer authorityProjectHql = new StringBuffer("");
		//系统管理员的项目资源配置权限、超级销售的数据查看权限
		if(this.superManager.contains(loginUser.getUserID()) 
				|| loginUserRoleIDList.contains(this.sysadmin) 
				|| loginUserRoleIDList.contains(this.superSale_siteOperation)) {
			//不做任何权限限制
		} else {
			//角色编号格式化
			String formatRoleIDList = StringUtils.formatIDListWithSyntax(loginUserRoleIDList, ",", "'");
			//构造权限匹配的项目资源查询
			String roleInfoHql = "select r.id from IRole r " +
					"where r.roleID in("+formatRoleIDList+")";
			authorityProjectHql.append("select distinct prc.projectUID from IProjectResourceConfig prc ");
			authorityProjectHql.append("where prc.userUID='" + loginUser.getId() + "' ");
			authorityProjectHql.append("and prc.roleUID in(" + roleInfoHql + ")");
		}
		return authorityProjectHql.toString();
	}
	
	/**
	 * 查询系统登录用户的权限匹配项目资源
	 * 注意：这是一个SQL限定查询语句，与底层数据库类型耦合，注意避免数模更改带来的系统问题
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-14
	 */
	public String queryAuthorityProjResSql() throws Exception {
		//查询系统用户的登录权限
		IUser loginUser = UserRoleUtil.getSysLoginUser();
		String loginUserRoleIDList = iRoleDao.queryRoleIDsByUserID(loginUser.getUserID());
		//构造权限匹配的项目资源
		StringBuffer authorityProjectHql = new StringBuffer("");
		//系统管理员的项目资源配置权限、超级销售的数据查看权限
		if(this.superManager.contains(loginUser.getUserID()) 
				|| loginUserRoleIDList.contains(this.sysadmin) 
				|| (loginUserRoleIDList.contains(this.superSale_siteOperation))) {
			//不做任何权限限制
		} else {
			//角色编号格式化
			String formatRoleIDList = StringUtils.formatIDListWithSyntax(loginUserRoleIDList, ",", "'");
			//构造权限匹配的项目资源查询
			String roleInfoHql = "select r.t_uuid from i_role r " +
					"where r.t_roleID in("+formatRoleIDList+")";
			authorityProjectHql.append("select distinct prc.t_projectUID from auth_projRes_config prc ");
			authorityProjectHql.append("where prc.t_userUID='" + loginUser.getId() + "' ");
			authorityProjectHql.append("and prc.t_roleUID in(" + roleInfoHql + ")");
		}
		return authorityProjectHql.toString();
	}
	
	/**
	 * @return the iProjectResourceConfigDao
	 */
	public IProjectResourceConfigDao getiProjectResourceConfigDao() {
		return iProjectResourceConfigDao;
	}

	/**
	 * @param iProjectResourceConfigDao the iProjectResourceConfigDao to set
	 */
	public void setiProjectResourceConfigDao(
			IProjectResourceConfigDao iProjectResourceConfigDao) {
		this.iProjectResourceConfigDao = iProjectResourceConfigDao;
	}

	/**
	 * @return the iRoleDao
	 */
	public IRoleDao getiRoleDao() {
		return iRoleDao;
	}

	/**
	 * @param iRoleDao the iRoleDao to set
	 */
	public void setiRoleDao(IRoleDao iRoleDao) {
		this.iRoleDao = iRoleDao;
	}

	/**
	 * @return the superManager
	 */
	public String getSuperManager() {
		return superManager;
	}

	/**
	 * @param superManager the superManager to set
	 */
	public void setSuperManager(String superManager) {
		this.superManager = superManager;
	}

	/**
	 * @return the sysadmin
	 */
	public String getSysadmin() {
		return sysadmin;
	}

	/**
	 * @param sysadmin the sysadmin to set
	 */
	public void setSysadmin(String sysadmin) {
		this.sysadmin = sysadmin;
	}

	/**
	 * @return the superSale_siteOperation
	 */
	public String getSuperSale_siteOperation() {
		return superSale_siteOperation;
	}

	/**
	 * @param superSale_siteOperation the superSale_siteOperation to set
	 */
	public void setSuperSale_siteOperation(String superSale_siteOperation) {
		this.superSale_siteOperation = superSale_siteOperation;
	}
	
}
